/**
 * Created by Snow on 7/26/2017.
 */

const _sourceBomList = [];

const _itemDataList = [];


//  ------------ 赋值 ------------------------

new Vue({
    el: '#app',
    data: function() {
        return {
            activeName: 'first',
            selectBomDetail:[],
            destBomDetail:[],
            tForm:{
                bom:"",
                version:"",
                description:""
            },
            sForm:{
                bom:"",
                version:"",
                description:""
            },
            bomComponentDialog: false,
            itemBomSearchDialog:false,
            sourceBomData: [],
            bomForm:{
                bom:'',
                bomDesc:''
            },
            newItemForm: {
                item: '',
                version: '',
                desc: ''
            },
            itemData : _itemDataList,
            splitBomData : [{
                id:"BOM:TEST,BV007,D",
                label: 'BV007/D',
                children: []
            }]
        }
    },
    methods: {
        ajaxPostRequest( params, callBackFun )
        {
            var me = this, _url = "http://localhost:8082/route";
            axios({
                method: 'post',
                url: _url,
                params: params
            }).then(function(response){
                if( response.status == 200 ){
                    callBackFun( response.data );
                }else{
                    console.log( response );
                }
            }).catch(function(err){
                console.log(err);
            });
        },
        ajaxGetRequest(  params, callBackFun ){
            var me = this, _url = "http://localhost:8082/route";
            axios.get( _url,{
                params: params
            }).then(function(response){
                if( response.status == 200 ){
                    callBackFun( response.data );
                }else{
                    console.log( response );
                }
            }).catch(function(err){
                console.log(err);
            });
        },
        query(){
            var me = this,_form = me.sForm;
            if( _form.bom == "" ){
                me.$notify.warning({
                    title: '提醒',
                    message: '请输入物料清单号'
                });
                return;
            };
            if( _form.version == "" ){
                me.$notify.warning({
                    title: '提醒',
                    message: '请输入物料清单版本'
                });
                return;
            };
            var param = {
                serviceName:"exposeBomService",
                serviceMethodName:"bomListByBom",
                plant:"TEST",
                id:_form.bom,
                version:_form.version
            };
            function callBack( data ){
                var bomComList = data.retObj.BomComList;
                var bomList = data.retObj.BomList;
                var bomObj = bomList[0];
                document.getElementById("bomIdHidden").value = bomObj.quid;
                if(bomComList.length>0){
                    me._itemDataList = [];
                    for(var i=0;i<bomComList.length;i++){
                        _itemDataList.push({
                            "id":bomComList[i].id,
                            "sequence": bomComList[i].sequence,
                            "assyItem": bomComList[i].materialFk,
                            "assyQty": bomComList[i].qty,
                            "measure": ""
                        });
                    }
                }
            }
            me.ajaxGetRequest( param,callBack );
        },
        itemBomSearch(){
            var me = this;
            var id = me.sForm.bom;
            var param = {
                serviceName:"exposeBomService",
                serviceMethodName:"bomListByBom",
                plant:"TEST",
                id:id,
                version:""
            };
            function callBack( data ){
                if( data.retObj.BomList.length >0 ){
                    me.sourceBomData = data.retObj.BomList;
                }else{
                    me.sourceBomData = [];
                }
                me.itemBomSearchDialog = true;
            };
            me.ajaxGetRequest( param,callBack );
        },
        saveBomData(){
            var me = this,_form = me.tForm;
            var param = {
                serviceName:"exposeBomService",
                serviceMethodName:"addBomData",
                plant:"TEST",
                id:_form.bom,
                adopted:"",
                bomType: "FICTITIOUS",
                curVersion: "N",
                version :_form.version,
                name : _form.description,
                bomTem :"",
                statusFk :"V",
                dateEffect :"",
                dateExpire :""
            };
            function callBack( data ){
                if(data.retCode==="1"){
                    me.splitBomData.push({
                        id:"BOM:"+"TEST"+","+_form.bom+","+_form.version,
                        label: _form.description,
                        children: []
                    });
                    me.$notify({
                        title: '消息',
                        message: '保存成功!',
                        type: 'success'
                    });
                }else{
                    me.$notify.error({
                        title: '保存出错',
                        message: data.retMsg
                    });
                }
            };
            me.ajaxGetRequest( param,callBack );
        },
        checkChange(obj,isChecked,children){
            if(isChecked){
                this.$refs.bomTree.setCheckedKeys([]);
                this.$refs.bomTree.setCheckedKeys([obj.id]);
            }
        },
        tableRowClassName(row, index) {
            if (index === 1) {
                return 'info-row';
            } else if (index === 3) {
                return 'positive-row';
            }
            return '';
        },
        remove() {

        },
        selectBomDetailChange( selection ){
            var me = this;
            me.selectBomDetail = selection;
        },
        choseDestBom(data, checked, indeterminate){
            var me = this;
            me.destBomDetail = data;
        },
        removeToComponent(){
            var me = this;
            var souBomCompList = me.selectBomDetail;
            var destBomDetailList = me.destBomDetail;
            if(souBomCompList.length==0){
                me.$notify.warning({
                    title: '提示',
                    message: '清选择要移动的组件'
                });
                return;
            }else if(destBomDetailList==0){
                me.$notify.warning({
                    title: '提示',
                    message: '清选择要放置的节点'
                });
                return;
            }else{
                var destBomId = destBomDetailList.id;
                var sourCompIdString = "";
                for(var z=0;z<souBomCompList.length;z++){
                    var id = souBomCompList[z].id;
                    if( z==0 ){
                        sourCompIdString = id;
                    }else{
                        sourCompIdString = sourCompIdString + "&" + id ;
                    }
                };
                // ------------------------------------- 后台执行方法体开始 ------------------------------------
                var param = {
                    serviceName:"exposeBomService",
                    serviceMethodName:"reDisBomComp",
                    souBomCompId:sourCompIdString,
                    destBomId:destBomId
                };
                function callBack( data ){
                    if(data.retCode==="1"){
                        var newCompIdList = data.retObj.newCompIdList;
                        var mTree = me.$refs.bomTree.getCheckedNodes();
                        var mTreeNode = mTree[0];
                        if(newCompIdList.length>0){
                            for(var j=0;j<newCompIdList.length;j++){
                                var newCompId = newCompIdList[j];
                                mTreeNode.children.push ({ id: newCompId, label: sourCompIdString, children: [] });
                            }
                            me.$notify({
                                title: '消息',
                                message: '移动成功!',
                                type: 'success'
                            });
                        }
                    }else{
                        me.$notify.error({
                            title: '移动出错',
                            message: data.retMsg
                        });
                    }
                };
                me.ajaxGetRequest( param,callBack );
                // -----------------------  后台执行方法体结束------------------------------------------------
            }
        },
        createNewItem(row, event) {
            this.bomComponentDialog=false;
            var me = this;
            var item = this.newItemForm.item;
            var version = this.newItemForm.version;
            var desc = this.newItemForm.desc;
            var showDesc = item+"/"+desc
            // ---- 树形 -------------------
            var mTree = this.$refs.splitBomTree.getCheckedNodes();
            if( mTree.length>0 ){
                var mTreeNode = mTree[0];
                var parentBomId = mTreeNode.id;
                // ------------------------------------- 后台执行方法体开始 ------------------------------------
                var param = {
                    serviceName:"exposeBomService",
                    serviceMethodName:"createNewVisionBom",
                    id:item,
                    name:desc,
                    version:version,
                    parentBomId: parentBomId,
                    plant:"TEST"
                };
                function callBack( data ){
                    if(data.retCode==="1"){
                        var bomId = "BOM:"+"TEST"+","+item+","+version;
                        mTreeNode.children.push ({ id: bomId, label: desc, children: [] });
                        me.$notify({
                            title: '消息',
                            message: '创建成功!',
                            type: 'success'
                        });
                    }else{
                        me.$notify.error({
                            title: '保存出错',
                            message: data.retMsg
                        });
                    }
                };
                me.ajaxGetRequest( param,callBack );
                // -----------------------  后台执行方法体结束------------------------------------------------

            }else{
                const h = this.$createElement;
                this.$notify.info({
                    title: '提示',
                    message: h('i', { style: 'color: teal'}, '请选择要放置的节点')
                });
            }
        },
        selectedRowBom(row, event) {
            var bom = row.bom;
            var version = row.version;
            var id = row.id;
            var name = row.name;
            this.itemBomSearchDialog=false;
            this.sForm.bom = id;
            this.sForm.version = version;
            this.sForm.description = name;
        }
    }
})